package leetcode_1001_1100;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class LeeCode_1079 {
    public static void main(String[] args) {
        System.out.println(numTilePossibilities("AAB"));
    }
    private static int numTilePossibilities(String tiles) {
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < tiles.length(); i++) {
            map.put(tiles.charAt(i), map.getOrDefault(tiles.charAt(i), 0) + 1);
        }
        Set<Character> set = new HashSet<>(map.keySet());
        return dfs(tiles.length(), map, set) - 1;
    }
    private static int dfs(int i, Map<Character, Integer> map, Set<Character> set){
        if (i == 0)
            return 1;
        int res = 1;
        for (Character key : set) {
            if (map.get(key) > 0){
                map.put(key, map.get(key) - 1);
                res += dfs(i - 1, map, set);
                map.put(key, map.get(key) + 1);
            }
        }
        return res;
    }
}
